package com.bole.controller;

import com.bole.data.Result;
import com.bole.dto.article.ArticleTypeDTO;
import com.bole.feign.ArticleTypeFeign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("articleType")
public class ArticleTypeController {

    @Resource
    private ArticleTypeFeign articleTypeFeign;

    /**
     * 获取资讯分类列表
     * @return
     */
    @GetMapping
    public Result listArticleType() {
        try {
            return articleTypeFeign.listArticleType();
        } catch (Exception e) {
            return Result.fail().setData("获取列表错误");
        }
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findArticleTypeById(@PathVariable("id") Integer id) {
        try {
            return articleTypeFeign.findArticleTypeById(id);
        } catch (Exception e) {
            return Result.fail().setData("根据id获取资讯分类错误");
        }
    }

    @PostMapping
    public Result saveArticleType(String title) {
        ArticleTypeDTO articleTypeDTO = new ArticleTypeDTO();
        articleTypeDTO.setTitle(title);
        return articleTypeFeign.saveArticleType(articleTypeDTO);
    }

    @PutMapping("/id")
    public Result updateArticleType(@PathVariable("id") Integer id,@RequestBody ArticleTypeDTO articleTypeDTO){
        return articleTypeFeign.updateArticleType(id,articleTypeDTO);
    }
}
